<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar">
        <!-- 这里是状态栏 -->
      </view>
      <view class="jtop_l" @tap="navigateBack">
        <image src="../../static/img/ic_back.png"></image>
      </view>
      <view class="jtop_c">我的名片</view>
    </view>
    <view class="tophr"></view>

    <view class="um_top">
      <text class="um_top_t">请加我的墨斗名片</text>
      <image
        :src="
          ma
            ? ma
            : 'https://bytes1024.oss-cn-hangzhou.aliyuncs.com/passport/1598340233433.png'
        "
        class="um_top_c"
      ></image>
      <text class="um_top_f">墨斗</text>
    </view>

    <view class="um_f">
      <view class="um_fc">
        <image src="../../static/img/user/ic_wechat_1.png"></image>
        <text>发送到微信</text>
      </view>
      <view class="um_fc" @tap="saveHttpImg(ma)">
        <image src="../../static/img/user/ic_download.png"></image>
        <text>保存到相册</text>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState, mapMutations } from 'vuex';
import permision from '@/components/wa-permission/permission.js';
// 引入二维码库
import QR from '@/components/wxqrcode.js'; // 二维码生成器

export default {
  computed: {
    ...mapState(['hasLogin', 'forcedLogin']),
  },
  data() {
    return {
      repeat_click: true,
      ma:
        'https://bytes1024.oss-cn-hangzhou.aliyuncs.com/passport/1598340233433.png',
    };
  },
  onShow() {
    let img = QR.createQrCodeImg(
      uni.getStorageSync('user').userInfoResponse.id,
      {
        size: parseInt(300), //二维码大小
      },
    );
    this.ma = img;

    console.log(img);

    // request({
    // 	url: '/ucenter/qrcode',
    // 	method: "GET",
    //
    // 	success: (res) => {
    // 		// if (res.data.statusCode == 200) {
    // 		// 	this.ma = res.data.data;
    // 		// } else {
    // 		// 						this.repeat_click = true;
    //uni.showToast({
    // 		// 		title: res.data.msg,
    // 		// 		icon: 'none'
    // 		// 	});
    // 		// }
    // 	},
    // 	fail: () => {
    // 							this.repeat_click = true;
    // uni.showToast({
    // 			title: '服务器开小差，请稍后~',
    // 			icon: 'none'
    // 		});
    // 		return false;
    // 	}
    // });
  },

  methods: {
    //下载网络图片
    saveHttpImg(url) {
      let _t = this;
      uni.downloadFile({
        url: url, //仅为示例，并非真实的资源
        success: res => {
          if (res.statusCode === 200) {
            let file = res.tempFilePath;
            _t.save(file);
          }
        },
      });
    },
    //保存图片到本地
    save(url) {
      permision.judgeIosPermission('photoLibrary');

      // permision.gotoAppPermissionSetting()

      let _this = this;
      uni.showLoading({
        title: '保存中...',
      });
      uni.saveImageToPhotosAlbum({
        filePath: url,
        success: () => {
          uni.hideLoading();
          this.repeat_click = true;
          uni.showToast({
            title: '图片已保存',
            icon: 'success',
            duration: 2000,
          });
        },
        fail: () => {
          uni.hideLoading();
          this.repeat_click = true;
          uni.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 2000,
          });
        },
        complete: () => {},
      });
    },

    ...mapMutations(['logout']),
    bindLogin() {
      uni.navigateTo({
        url: '../login/login',
      });
    },
    bindLogout() {
      this.logout();
      /**
       * 如果需要强制登录跳转回登录页面
       */
      if (this.forcedLogin) {
        uni.reLaunch({
          url: '../login/login',
        });
      }
    },
    navigateBack() {
      uni.navigateBack();
    },
  },
};
</script>

<style>
.um_top {
  width: 730rpx;
  height: 1000rpx;
  display: block;
  float: left;
  margin: 30rpx 15rpx 40rpx;
  background: url(../../static/img/user/bj.png) no-repeat;
  background-size: 730rpx 1000rpx;
}

.um_top_c {
  width: 550rpx;
  height: 547rpx;
  display: block;
  float: left;
  margin: 80rpx 90rpx 0rpx;
}

.um_top_t {
  width: 100%;
  height: 44rpx;
  display: block;
  float: left;
  margin: 40rpx 0rpx 0rpx;
  font-size: 36rpx;
  line-height: 44rpx;
  text-align: center;
  color: #ffffff;
}

.um_top_f {
  width: 100%;
  height: 50rpx;
  display: block;
  float: left;
  margin: 160rpx 0rpx 0rpx;
  font-size: 36rpx;
  line-height: 50rpx;
  text-align: center;
  color: #000000;
}

.um_f {
  width: 687rpx;
  height: 150rpx;
  display: block;
  float: left;
  background: #ffffff;
  box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.103201);
  border-radius: 12rpx;
  margin: 0 31rpx 30rpx;
  background: #ffffff;
}

.um_fc {
  width: 50%;
  height: 150rpx;
  display: block;
  float: left;
}

.um_fc text {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  font-size: 24rpx;
  line-height: 32rpx;
  text-align: center;
  color: #999999;
}

.um_fc image {
  width: 64rpx;
  height: 64rpx;
  display: block;
  float: none;
  margin: 27rpx auto 0rpx;
}

.ueb {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  margin: 20rpx 0 0 0;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  background: #ffffff;
}

.ueb_logo {
  width: 304rpx;
  height: 250rpx;
  display: block;
  float: none;
  margin: 20rpx auto 0;
}

.uebs {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  margin: 20rpx 0 0 0;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  background: #ffffff;
}

.uebsb {
  width: 720rpx;
  margin: 0 0 0 30rpx;
  height: 90rpx;
  display: block;
  float: left;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}

.uebsb_l {
  width: 150rpx;
  height: auto;
  display: block;
  float: left;
  margin: 24rpx 0;
  font-size: 34rpx;
  color: #000000;
}

.uebsb_lr {
  width: 500rpx;
  height: auto;
  display: block;
  float: right;
  margin: 24rpx 30rpx 0 0;
  font-size: 34rpx;
  color: #000000;
}

.uebsb_lr image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin: 0rpx 0 -10rpx 10rpx;
}

.uebsb_lr2 {
  font-size: 28rpx;
  margin-top: 30rpx;
}

.uebsb_lr3 {
  color: #0056ff;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}

.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}

.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}

.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}

.jtop_lt {
  margin: 0 0 0 10rpx;
}

.jtop_r {
  width: auto;
  height: 88rpx;
  display: block;
  float: right;
  margin: 0rpx 20rpx 0 0;
  line-height: 88rpx;
  font-size: 32rpx;
  color: #000000;
  color: #256aff;
}

.jtop_rl {
  margin: 20rpx 30rpx 0 0;
}

.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}

.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}

.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}

.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}

.tophr {
  width: 100%;
  height: 88rpx;
}
</style>
